import { ethers, network } from "hardhat";

async function main() {
  if (network.name !== "localhost" && network.name !== "hardhat") {
    throw new Error("Use --network localhost for this script");
  }

  const [deployer, operator, treasury, injector] = await ethers.getSigners();

  console.log("=== 彩票状态检查 ===");
  console.log("网络:", network.name);
  console.log("操作员账户:", operator.address);
  console.log("金库账户:", treasury.address);
  console.log("注入账户:", injector.address);

  // 获取已部署的合约地址
  const LOTTERY_ADDRESS = "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"; // Lottery 地址
  console.log("彩票合约地址:", LOTTERY_ADDRESS);

  // 获取彩票合约实例
  const lottery = await ethers.getContractAt("PancakeSwapLottery", LOTTERY_ADDRESS);

  // 检查当前彩票ID
  const currentLotteryId = await lottery.viewCurrentLotteryId();
  console.log("\n=== 当前彩票信息 ===");
  console.log("当前彩票ID:", currentLotteryId.toString());

  if (currentLotteryId.eq(0)) {
    console.log("❌ 还没有彩票被创建");
    return;
  }

  // 获取当前彩票详细信息
  const currentLottery = await lottery.viewLottery(currentLotteryId);
  
  // 状态映射
  const statusMap = {
    0: "PENDING",
    1: "OPEN", 
    2: "CLOSE",
    3: "CLAIMABLE"
  };

  console.log("彩票状态:", statusMap[currentLottery.status]);
  console.log("开始时间:", new Date(currentLottery.startTime.toNumber() * 1000).toLocaleString());
  console.log("结束时间:", new Date(currentLottery.endTime.toNumber() * 1000).toLocaleString());
  console.log("票价:", ethers.utils.formatEther(currentLottery.priceTicketInCake), "CAKE");
  console.log("折扣除数:", currentLottery.discountDivisor.toString());
  console.log("金库费用:", currentLottery.treasuryFee.toString(), "(除以100得到百分比)");
  console.log("已收集金额:", ethers.utils.formatEther(currentLottery.amountCollectedInCake), "CAKE");
  console.log("第一张票ID:", currentLottery.firstTicketId.toString());
  console.log("下一轮第一张票ID:", currentLottery.firstTicketIdNextLottery.toString());
  console.log("最终号码:", currentLottery.finalNumber.toString());

  // 计算剩余时间
  const now = Math.floor(Date.now() / 1000);
  const timeRemaining = currentLottery.endTime.toNumber() - now;
  
  if (timeRemaining > 0) {
    const hours = Math.floor(timeRemaining / 3600);
    const minutes = Math.floor((timeRemaining % 3600) / 60);
    const seconds = timeRemaining % 60;
    console.log("剩余时间:", `${hours}小时 ${minutes}分钟 ${seconds}秒`);
  } else {
    console.log("彩票已结束");
  }

  // 显示奖励分配
  console.log("\n=== 奖励分配 ===");
  currentLottery.rewardsBreakdown.forEach((reward, index) => {
    console.log(`${index + 1}个数字匹配: ${reward.toString()} (${reward.toNumber() / 100}%)`);
  });

  // 显示各等级奖金
  console.log("\n=== 各等级奖金 ===");
  currentLottery.cakePerBracket.forEach((cake, index) => {
    console.log(`${index + 1}个数字匹配: ${ethers.utils.formatEther(cake)} CAKE`);
  });

  // 显示各等级中奖人数
  console.log("\n=== 各等级中奖人数 ===");
  currentLottery.countWinnersPerBracket.forEach((count, index) => {
    console.log(`${index + 1}个数字匹配: ${count.toString()} 人`);
  });

  // 检查是否可以开始新彩票
  console.log("\n=== 新彩票检查 ===");
  if (currentLottery.status === 3) { // CLAIMABLE
    console.log("✅ 可以开始新彩票");
  } else {
    console.log("❌ 还不能开始新彩票，当前状态:", statusMap[currentLottery.status]);
  }

  // 显示合约配置
  console.log("\n=== 合约配置 ===");
  const maxNumberTicketsPerBuyOrClaim = await lottery.getmaxNumberTicketsPerBuyOrClaim();
  console.log("每次最多购买票数:", maxNumberTicketsPerBuyOrClaim.toString());
  
  const maxPriceTicketInCake = await lottery.maxPriceTicketInCake();
  const minPriceTicketInCake = await lottery.minPriceTicketInCake();
  console.log("票价范围:", `${ethers.utils.formatEther(minPriceTicketInCake)} - ${ethers.utils.formatEther(maxPriceTicketInCake)} CAKE`);
  
  const pendingInjectionNextLottery = await lottery.pendingInjectionNextLottery();
  console.log("下一轮注入金额:", ethers.utils.formatEther(pendingInjectionNextLottery), "CAKE");

  // 显示常量
  console.log("\n=== 合约常量 ===");
  console.log("最小彩票长度: 4小时 - 5分钟");
  console.log("最大彩票长度: 4天 + 5分钟");
  console.log("最小折扣除数: 300");
  console.log("最大金库费用: 3000 (30%)");

  // 检查当前时间
  console.log("\n=== 时间信息 ===");
  console.log("当前时间:", new Date().toLocaleString());
  console.log("当前时间戳:", now);
  
  if (currentLottery.status === 1) { // OPEN
    const timeUntilEnd = currentLottery.endTime.toNumber() - now;
    if (timeUntilEnd > 0) {
      const hours = Math.floor(timeUntilEnd / 3600);
      const minutes = Math.floor((timeUntilEnd % 3600) / 60);
      console.log("⏰ 彩票销售中，距离结束还有:", `${hours}小时 ${minutes}分钟`);
    } else {
      console.log("⏰ 彩票销售时间已结束，等待关闭");
    }
  } else if (currentLottery.status === 2) { // CLOSE
    console.log("⏰ 彩票已关闭，等待开奖");
  } else if (currentLottery.status === 3) { // CLAIMABLE
    console.log("⏰ 彩票已开奖，可以领取奖金");
  }
}

main().catch((e) => {
  console.error('检查彩票状态失败', e);
  process.exit(1);
});
